/*     */ package nccloud.pubimpl.riart.queryschenme;
/*     */ 
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;

/*     */ import nc.bs.bd.baseservice.md.VOArrayUtil;
/*     */ import nc.bs.dao.BaseDAO;
/*     */ import nc.bs.framework.common.InvocationInfoProxy;
/*     */ import nc.bs.framework.common.RuntimeEnv;
/*     */ import nc.impl.pubapp.pattern.database.IDExQueryBuilder;
/*     */ import nc.jdbc.framework.SQLParameter;
/*     */ import nc.vo.pub.BusinessException;
/*     */ import nc.vo.pub.IAttributeMeta;
/*     */ import nc.vo.pub.lang.UFBoolean;
/*     */ import nc.vo.pub.lang.UFDateTime;
/*     */ import nc.vo.pubapp.pattern.model.tool.MetaTool;
/*     */ import nccloud.commons.collections.CollectionUtils;
/*     */ import nccloud.commons.lang.ArrayUtils;
/*     */ import nccloud.commons.lang.StringUtils;
/*     */ import nccloud.dto.riart.querytemplate.queryscheme.NCCQuerySchemeConstants;
/*     */ import nccloud.dto.riart.querytemplate.queryscheme.NCCQuerySchemeDTO;
/*     */ import nccloud.dto.riart.querytemplate.queryscheme.NCCQuerySchemeUserVO;
/*     */ import nccloud.dto.riart.querytemplate.queryscheme.NCCQuerySchemeVO;
/*     */ import nccloud.pubitf.riart.queryschenme.IQuerySchemeService4NCC;
/*     */ 
/*     */ 
/*     */ public class QuerySchemeService4NCCImpl
/*     */   implements IQuerySchemeService4NCC
/*     */ {
/*  36 */   private BaseDAO getBasedao() { return new BaseDAO(); }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public NCCQuerySchemeVO insertQS(NCCQuerySchemeVO vo) throws BusinessException {
/*  41 */     vo.setCuserid(InvocationInfoProxy.getInstance().getUserId());
/*  42 */     String pk = getBasedao().insertVO(vo);
/*  43 */     vo.setPk_ncc_queryscheme(pk);
/*  44 */     return vo;
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public NCCQuerySchemeDTO insertQSAndIsDefaultDisplay(NCCQuerySchemeVO vo, UFBoolean isDefaultDisplay) throws BusinessException {
/*  50 */     NCCQuerySchemeDTO dto = new NCCQuerySchemeDTO();
/*  51 */     NCCQuerySchemeVO newQS = insertQS(vo);
/*  52 */     if (isDefaultDisplay.booleanValue()) {
/*  53 */       NCCQuerySchemeUserVO QSUserVO = setQSDefaultdisplay(vo, InvocationInfoProxy.getInstance().getUserId());
/*  54 */       dto.setQuerySchemeUserVO(QSUserVO);
/*     */     } 
/*  56 */     dto.setQuerySchemeVO(newQS);
/*  57 */     return dto;
/*     */   }
/*     */ 
/*     */   
/*     */   private NCCQuerySchemeVO copyFromPreparedScheme(NCCQuerySchemeVO parentVO) throws BusinessException {
/*  62 */     parentVO.setParentid(parentVO.getPk_ncc_queryscheme());
/*  63 */     parentVO.setPk_ncc_queryscheme(null);
/*  64 */     parentVO.setIsprepared(UFBoolean.valueOf(false));
/*  65 */     parentVO.setCuserid(InvocationInfoProxy.getInstance().getUserId());
/*  66 */     parentVO.setPk_org(InvocationInfoProxy.getInstance().getGroupId());
/*  67 */     parentVO.setIsdelete(UFBoolean.valueOf(false));
/*  68 */     return parentVO;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public NCCQuerySchemeUserVO setQSDefaultdisplay(NCCQuerySchemeVO vo, String usedCuserid) throws BusinessException {
/*  75 */     String deleteSqlCondition = "areaid = ? and usedcuserid = ?";
/*  76 */     SQLParameter params = new SQLParameter();
/*  77 */     params.addParam(vo.getAreaid());
/*  78 */     params.addParam(usedCuserid);
/*  79 */     getBasedao().deleteByClause(NCCQuerySchemeUserVO.class, deleteSqlCondition, params);
/*     */     
/*  81 */     NCCQuerySchemeUserVO QSUserVO = new NCCQuerySchemeUserVO();
/*  82 */     QSUserVO.setPk_ncc_queryscheme(vo.getPk_ncc_queryscheme());
/*  83 */     QSUserVO.setUsedcuserid(usedCuserid);
/*  84 */     QSUserVO.setAreaid(vo.getAreaid());
/*  85 */     QSUserVO.setIsdefaultdisplay(UFBoolean.TRUE);
/*  86 */     String pk = getBasedao().insertVO(QSUserVO);
/*  87 */     QSUserVO.setPk_queryscheme_user(pk);
/*     */     
/*  89 */     return QSUserVO;
/*     */   }
/*     */ 
/*     */   
/*     */   public NCCQuerySchemeVO updateQS(NCCQuerySchemeVO qsvo) throws BusinessException {
/*  94 */     qsvo.setConditionobj4Blob(qsvo.getConditionobj4web());
/*  95 */     qsvo.setRenderobj4Blob(qsvo.getRenderobj4web());
/*     */     qsvo.setCuserid(InvocationInfoProxy.getInstance().getUserId());
/*     */	  
/*  97 */     if (RuntimeEnv.getInstance().isDevelopMode()) {
/*  98 */       getBasedao().updateVO(qsvo);
/*  99 */       return qsvo;
/*     */     } 
/*     */     
/* 102 */     NCCQuerySchemeVO vo = getNCCQuerySchemeVOsByPK(qsvo.getPk_ncc_queryscheme());
/*     */     
/* 104 */     if (vo.getIsprepared().booleanValue()) {
/*     */ 
/*     */       
/* 107 */       NCCQuerySchemeVO copy_vo = copyFromPreparedScheme(vo);
/* 108 */       copy_vo.setName((qsvo.getName() == null) ? vo.getName() : qsvo.getName());
/* 109 */       copy_vo.setName2(null);
/* 110 */       copy_vo.setName3(null);
/* 111 */       copy_vo.setName4(null);
/* 112 */       copy_vo.setName5(null);
/* 113 */       copy_vo.setName6(null);
/* 114 */       String pk = getBasedao().insertVO(copy_vo);
/* 115 */       copy_vo.setPk_ncc_queryscheme(pk);
/* 116 */       return copy_vo;
/*     */     } 
/* 118 */     qsvo.setTs(new UFDateTime());
/* 119 */     qsvo.setIsmodified(UFBoolean.valueOf(true));
/* 120 */     qsvo.setIsdelete(UFBoolean.valueOf(false));
/* 121 */     qsvo.setIsprepared(UFBoolean.valueOf(false));
/* 122 */     qsvo.setParentid(vo.getParentid());
/* 123 */     qsvo.setSequenc(vo.getSequenc());
/* 124 */     qsvo.setCuserid(InvocationInfoProxy.getInstance().getUserId());
/* 125 */     qsvo.setPk_org(InvocationInfoProxy.getInstance().getGroupId());
/* 126 */     getBasedao().updateVO(qsvo);
/* 127 */     return qsvo;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/* 133 */   public NCCQuerySchemeVO[] updateQS(NCCQuerySchemeVO[] vos) throws BusinessException { return null; }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/* 139 */   public NCCQuerySchemeVO updateQSSequence(NCCQuerySchemeVO vo) throws BusinessException { return null; }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/* 145 */   public NCCQuerySchemeVO[] updateQSSequence(NCCQuerySchemeVO[] vos) throws BusinessException { return null; }
/*     */ 
/*     */ 
/*     */   
/*     */   public void deleteQS(NCCQuerySchemeVO vo) throws BusinessException {
/* 150 */     getBasedao().deleteByPK(NCCQuerySchemeVO.class, vo.getPk_ncc_queryscheme());
/*     */     
/* 152 */     SQLParameter param = new SQLParameter();
/* 153 */     param.addParam(vo.getPk_ncc_queryscheme());
/* 154 */     getBasedao().deleteByClause(NCCQuerySchemeUserVO.class, "pk_ncc_queryscheme = ?", param);
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public void deleteQueryScheme(String[] pks) throws BusinessException {}
/*     */ 
/*     */ 
/*     */   
/*     */   public NCCQuerySchemeVO[] getNCCQuerySchemeVOsBy(String areaid, String userid) throws BusinessException {
/* 164 */     if (isEmpty(areaid)) {
/* 165 */       return null;
/*     */     }
/* 167 */     String where = constructWhereSQL(areaid, userid);
/* 168 */     IAttributeMeta[] queryAttributes = MetaTool.getVOMeta(NCCQuerySchemeVO.class).getStatisticInfo().getPerisistentAttributes();
/* 169 */     List<String> lists = new ArrayList<String>();
/* 170 */     for (IAttributeMeta field : queryAttributes) {
/* 171 */       String name = field.getColumn().getName();
/* 172 */       lists.add(name);
/*     */     } 
/* 174 */     Collection<NCCQuerySchemeVO> results = getBasedao().retrieveByClause(NCCQuerySchemeVO.class, where, lists.toArray(new String[0]));
/* 175 */     if (isEmpty(results)) {
/* 176 */       return null;
/*     */     }
/* 178 */     filter(results);
/*     */     
/* 180 */     Iterator<NCCQuerySchemeVO> it = results.iterator();
/* 181 */     while (it.hasNext()) {
/* 182 */       NCCQuerySchemeVO qsvo = it.next();
/* 183 */       String mult_name = qsvo.getMultiLangName().toString();
/* 184 */       if (StringUtils.isEmpty(mult_name)) {
/* 185 */         mult_name = qsvo.getName();
/*     */       }
/* 187 */       if (StringUtils.isEmpty(mult_name)) {
/* 188 */         mult_name = qsvo.getName2();
/*     */       }
/* 190 */       if (StringUtils.isEmpty(mult_name)) {
/* 191 */         mult_name = qsvo.getName3();
/*     */       }
/* 193 */       if (StringUtils.isEmpty(mult_name)) {
/* 194 */         mult_name = qsvo.getName4();
/*     */       }
/* 196 */       if (StringUtils.isEmpty(mult_name)) {
/* 197 */         mult_name = qsvo.getName5();
/*     */       }
/* 199 */       if (StringUtils.isEmpty(mult_name)) {
/* 200 */         mult_name = qsvo.getName6();
/*     */       }
/* 202 */       qsvo.setName(mult_name);
/*     */     } 
/*     */ 
/*     */     
/* 206 */     return results.toArray(new NCCQuerySchemeVO[0]);
/*     */   }
/*     */   
/*     */   public NCCQuerySchemeVO[] getNCCQuerySchemeVOs(String areaid, String userid) throws BusinessException {
/* 210 */     if (isEmpty(areaid)) {
/* 211 */       return null;
/*     */     }
/* 213 */     String where = constructWhereSQL(areaid, userid);
/* 214 */     IAttributeMeta[] queryAttributes = MetaTool.getVOMeta(NCCQuerySchemeVO.class).getStatisticInfo().getPerisistentAttributes();
/* 215 */     List<String> lists = new ArrayList<String>();
/* 216 */     for (IAttributeMeta field : queryAttributes) {
/* 217 */       String name = field.getColumn().getName();
/* 218 */       lists.add(name);
/*     */     } 
/* 220 */     Collection<NCCQuerySchemeVO> results = getBasedao().retrieveByClause(NCCQuerySchemeVO.class, where, lists.toArray(new String[0]));
/* 221 */     if (isEmpty(results)) {
/* 222 */       return null;
/*     */     }
/* 224 */     filter(results);
/*     */ 
/*     */ 
/*     */     
/* 228 */     return results.toArray(new NCCQuerySchemeVO[0]);
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public NCCQuerySchemeDTO[] getQSDTOByAreaAndUser(String areaid, String userid) throws BusinessException {
/* 234 */     NCCQuerySchemeVO[] QSVOs = getNCCQuerySchemeVOsBy(areaid, userid);
/* 235 */     if (ArrayUtils.isEmpty(QSVOs)) {
/* 236 */       return null;
/*     */     }
/* 238 */     List<NCCQuerySchemeDTO> results = new ArrayList<NCCQuerySchemeDTO>();
/* 239 */     List<String> QSPKs = new ArrayList<String>(Arrays.asList(VOArrayUtil.getPrimaryKeyArray(QSVOs)));
/* 240 */     QSPKs.add(NCCQuerySchemeConstants.VIRTUAL_QUERYSCHEME_PK);
/*     */     
/* 242 */     String sqlCond = (new IDExQueryBuilder(NCCQuerySchemeConstants.QUERYSCHEME_TEMPTABLEPREFIX)).buildSQL("pk_ncc_queryscheme", QSPKs.toArray(new String[0]));
/* 243 */     sqlCond = sqlCond + " and areaid = ? and usedcuserid = ?";
/* 244 */     SQLParameter params = new SQLParameter();
/* 245 */     params.addParam(areaid);
/* 246 */     params.addParam(userid);
/* 247 */     List<NCCQuerySchemeUserVO> QSUserVOList = (List)getBasedao().retrieveByClause(NCCQuerySchemeUserVO.class, sqlCond, params);
/*     */     
/* 249 */     Map<String, NCCQuerySchemeUserVO> map = new HashMap<String, NCCQuerySchemeUserVO>();
/* 250 */     if (!CollectionUtils.isEmpty(QSUserVOList)) {
/* 251 */       for (NCCQuerySchemeUserVO QSUserVO : QSUserVOList) {
/* 252 */         if (NCCQuerySchemeConstants.VIRTUAL_QUERYSCHEME_PK.equalsIgnoreCase(QSUserVO.getPk_ncc_queryscheme())) {
/*     */           
/* 254 */           NCCQuerySchemeDTO dto = new NCCQuerySchemeDTO();
/* 255 */           NCCQuerySchemeVO QSVO = new NCCQuerySchemeVO();
/* 256 */           QSVO.setPk_ncc_queryscheme(NCCQuerySchemeConstants.VIRTUAL_QUERYSCHEME_PK);
/* 257 */           QSVO.setAreaid(QSUserVO.getAreaid());
/* 258 */           dto.setQuerySchemeUserVO(QSUserVO);
/* 259 */           dto.setQuerySchemeVO(QSVO);
/* 260 */           results.add(dto); continue;
/*     */         } 
/* 262 */         map.put(QSUserVO.getPk_ncc_queryscheme(), QSUserVO);
/*     */       } 
/*     */     }
/*     */ 
/*     */     
/* 267 */     for (NCCQuerySchemeVO QSVO : QSVOs) {
/* 268 */       NCCQuerySchemeDTO dto = new NCCQuerySchemeDTO();
/* 269 */       dto.setQuerySchemeVO(QSVO);
/* 270 */       dto.setQuerySchemeUserVO(map.get(QSVO.getPk_ncc_queryscheme()));
/* 271 */       results.add(dto);
/*     */     } 
/* 273 */     return results.toArray(new NCCQuerySchemeDTO[0]);
/*     */   }
/*     */ 
/*     */   
/* 277 */   private static boolean isEmpty(String s) { return (s == null || s.trim().length() == 0); }
/*     */ 
/*     */ 
/*     */   
/* 281 */   private static <T> boolean isEmpty(Collection<T> c) { return (c == null || c.size() == 0); }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private String constructWhereSQL(String areaid, String userid) {
/* 287 */     StringBuilder where = new StringBuilder();
/* 288 */     where.append(" areaid ='" + areaid + "' ");
/* 289 */     if (isEmpty(userid)) {
/* 290 */       where.append(" and (cuserid = '~' or cuserid is null or def1 = 'Y') ");
/*     */     } else {
/* 292 */       where.append(" and (def1 = 'Y' or cuserid = '~' or cuserid is null or cuserid = '").append(userid).append("') ");
/*     */     } 
/* 294 */     where.append(" order by sequenc asc");
/*     */     
/* 296 */     return where.toString();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void filter(Collection<NCCQuerySchemeVO> qsvos) {
/* 304 */     filterPreparedSchemesAlreadyDerived(qsvos);
/* 305 */     filterDeletedSchemes(qsvos);
/*     */   }
/*     */   
/*     */   private void filterPreparedSchemesAlreadyDerived(Collection<NCCQuerySchemeVO> qsvos) {
/* 309 */     Iterator<NCCQuerySchemeVO> it = qsvos.iterator();
/* 310 */     while (it.hasNext()) {
/* 311 */       NCCQuerySchemeVO qsvo = it.next();
/* 312 */       if (qsvo.isPrepared() && isAlreadyDerived(qsvo, qsvos)) {
/* 313 */         it.remove();
/*     */       }
/*     */     } 
/*     */   }
/*     */   
/*     */   private boolean isAlreadyDerived(NCCQuerySchemeVO preparedScheme, Collection<NCCQuerySchemeVO> qsvos) {
/* 319 */     for (NCCQuerySchemeVO qsvo : qsvos) {
/*     */       
/* 321 */       if (preparedScheme.getPk_ncc_queryscheme().equals(qsvo.getParentid())) {
/* 322 */         return true;
/*     */       }
/*     */     } 
/* 325 */     return false;
/*     */   }
/*     */   
/*     */   private void filterDeletedSchemes(Collection<NCCQuerySchemeVO> qsvos) {
/* 329 */     Iterator<NCCQuerySchemeVO> it = qsvos.iterator();
/* 330 */     while (it.hasNext()) {
/* 331 */       NCCQuerySchemeVO qsvo = it.next();
/* 332 */       if (qsvo.getIsdelete() != null && qsvo.getIsdelete().booleanValue()) {
/* 333 */         it.remove();
/*     */       }
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */   
/* 340 */   public NCCQuerySchemeVO getNCCQuerySchemeVOsByPK(String pk_ncc_queryscheme) throws BusinessException { return (NCCQuerySchemeVO)getBasedao().retrieveByPK(NCCQuerySchemeVO.class, pk_ncc_queryscheme); }
/*     */ 
/*     */ 
/*     */   
/*     */   public void copyAddDefaultQS(String oldAreaPk, String newAreaPK) throws BusinessException {
/* 345 */     NCCQuerySchemeVO[] qsvos = getNCCQuerySchemeVOs(oldAreaPk, null);
/* 346 */     if (qsvos == null || qsvos.length < 1) {
/*     */       return;
/*     */     }
/*     */     
/* 350 */     Set<String> set = new HashSet<String>();
/* 351 */     for (NCCQuerySchemeVO qsvo : qsvos) {
/* 352 */       qsvo.setPk_ncc_queryscheme(null);
/* 353 */       qsvo.setAreaid(newAreaPK);
/* 354 */       qsvo.setCuserid("~");
/* 355 */       if (set.contains(qsvo.getName())) {
/* 356 */         throw new BusinessException("从区域主键为" + oldAreaPk + "中查出来多个名称为" + qsvo.getName() + "的查询方案，构建的数据有问题");
/*     */       }
/* 358 */       set.add(qsvo.getName());
/*     */     } 
/*     */     
/* 361 */     getBasedao().insertVOArray(qsvos);
/*     */   }
/*     */ 
/*     */   
/*     */   public NCCQuerySchemeVO[] getQSByParentID(String pk_parent) throws BusinessException {
/* 366 */     if (isEmpty(pk_parent)) {
/* 367 */       return null;
/*     */     }
/* 369 */     String where = "parentid = '" + pk_parent + "' and " + "isdelete" + " = 'N' ";
/*     */     
/* 371 */     Collection<NCCQuerySchemeVO> results = getBasedao().retrieveByClause(NCCQuerySchemeVO.class, where);
/* 372 */     if (isEmpty(results)) {
/* 373 */       return null;
/*     */     }
/* 375 */     return results.toArray(new NCCQuerySchemeVO[0]);
/*     */   }
/*     */ 
/*     */ 
/*     */   
/* 380 */   public void deleteQSByAreaID(String areaid) throws BusinessException { getBasedao().deleteByClause(NCCQuerySchemeVO.class, " areaid = '" + areaid + "' "); }
/*     */ }


/* Location:              D:\NC\Home\xmxz\home\modules\riart\META-INF\lib\riart_riartqueryTemplate_rtLevel-1.jar!/nccloud/pubimpl/riart/queryschenme/QuerySchemeService4NCCImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.0.7
 */